/**
 * 
 */
package com.xpec.c4.db.usertype;

import java.util.HashSet;
import java.util.Set;

import com.ms.db.usertype.BasicToString;
import com.xpec.c4.db.util.CodecTool;
import com.xpec.c4.game.utility.GUID;

/**
 * @author Eric_Wang
 *
 */
public class SetGUIDToString extends BasicToString {

	@Override
	public int[] sqlTypes() {
		return CLOB_TYPES;
	}

	@Override
	public Class<?> returnedClass() {
		return Set.class;
	}

	@Override
	protected String assemble(Object obj) {
		@SuppressWarnings("unchecked")
		Set<GUID> set = (Set<GUID>) obj;
		StringBuilder str = new StringBuilder();

		int len = set.size();
		str.append(len).append(SPLITTER);
		for (GUID guid : set) {
			CodecTool.append(str, guid.toString(), SPLITTER);
		}
		if (str.length() > 0) {
			str.setLength(str.length() - 1);
		}
		return str.toString();
	}

	@Override
	protected Object parse(String value) {
		if (value == null) {
			return null;
		}
		String[] strs = CodecTool.split(value, SPLITTER_RE);
		Set<GUID> set=new HashSet<GUID>();
		int index=0;
		int len=Integer.valueOf(strs[index++]);
		for(int i=0;i<len;i++){
			set.add(GUID.parseUUID(strs[index++]));
		}
		return set;
	}
	

}
